package com.city.java.report_videos.service.impl;

import com.city.java.report_videos.mapper.ReportVideoMapper;
import com.city.java.report_videos.pojo.entity.ReportVideos;
import com.city.java.base.response.JsonResult;
import com.city.java.base.response.StatusCode;
import com.city.java.report_videos.service.ReportVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 举报视频服务实现类
 */
@Slf4j
@Service
public class ReportVideoServiceImpl implements ReportVideoService {

    @Autowired
    private ReportVideoMapper reportVideoMapper;

    @Override
    public JsonResult addReportVideo(ReportVideos reportVideo) {
        log.debug("准备添加举报视频: {}", reportVideo);

        // 参数校验
        if (reportVideo == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报视频信息不能为空");
        }

        if (reportVideo.getReportId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报ID不能为空");
        }

        if (reportVideo.getVideoUrl() == null || reportVideo.getVideoUrl().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "视频URL不能为空");
        }

        // 设置创建时间
        if (reportVideo.getCreatedAt() == null) {
            reportVideo.setCreatedAt(LocalDateTime.now());
        }

        try {
            reportVideoMapper.insertReportVideo(reportVideo);
            log.info("举报视频添加成功: 举报视频ID={}", reportVideo.getReportVideoId());
            return JsonResult.ok();
        } catch (Exception e) {
            log.error("添加举报视频失败: 举报ID={}, 视频URL={}", reportVideo.getReportId(), reportVideo.getVideoUrl(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "添加举报视频失败");
        }
    }

    @Override
    public JsonResult getReportVideoById(Long reportVideoId) {
        log.debug("根据ID查询举报视频: {}", reportVideoId);

        // 参数校验
        if (reportVideoId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报视频ID不能为空");
        }

        try {
            ReportVideos reportVideo = reportVideoMapper.selectById(reportVideoId);
            if (reportVideo != null) {
                log.info("查询举报视频成功: ID={}", reportVideoId);
                return JsonResult.ok(reportVideo);
            } else {
                log.warn("举报视频不存在: ID={}", reportVideoId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报视频不存在");
            }
        } catch (Exception e) {
            log.error("查询举报视频失败: ID={}", reportVideoId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报视频失败");
        }
    }

    @Override
    public JsonResult getReportVideosByReportId(Long reportId) {
        log.debug("根据举报ID查询举报视频列表: {}", reportId);

        // 参数校验
        if (reportId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报ID不能为空");
        }

        try {
            List<ReportVideos> reportVideos = reportVideoMapper.selectByReportId(reportId);
            log.info("查询举报视频列表成功: 举报ID={}, 视频数量={}", reportId, reportVideos.size());
            return JsonResult.ok(reportVideos);
        } catch (Exception e) {
            log.error("查询举报视频列表失败: 举报ID={}", reportId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报视频列表失败");
        }
    }

    @Override
    public JsonResult getAllReportVideos() {
        log.debug("查询所有举报视频列表");

        try {
            List<ReportVideos> reportVideos = reportVideoMapper.selectAll();
            log.info("查询所有举报视频列表成功: 视频数量={}", reportVideos.size());
            return JsonResult.ok(reportVideos);
        } catch (Exception e) {
            log.error("查询所有举报视频列表失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询举报视频列表失败");
        }
    }

    @Override
    public JsonResult deleteReportVideoById(Long reportVideoId) {
        log.debug("根据ID删除举报视频: {}", reportVideoId);

        // 参数校验
        if (reportVideoId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报视频ID不能为空");
        }

        try {
            int result = reportVideoMapper.deleteById(reportVideoId);
            if (result > 0) {
                log.info("删除举报视频成功: ID={}", reportVideoId);
                return JsonResult.ok();
            } else {
                log.warn("删除举报视频失败，举报视频不存在: ID={}", reportVideoId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报视频不存在");
            }
        } catch (Exception e) {
            log.error("删除举报视频失败: ID={}", reportVideoId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除举报视频失败");
        }
    }

    @Override
    public JsonResult deleteReportVideosByReportId(Long reportId) {
        log.debug("根据举报ID删除举报视频: {}", reportId);

        // 参数校验
        if (reportId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报ID不能为空");
        }

        try {
            int result = reportVideoMapper.deleteByReportId(reportId);
            log.info("删除举报视频成功: 举报ID={}, 删除数量={}", reportId, result);
            return JsonResult.ok();
        } catch (Exception e) {
            log.error("删除举报视频失败: 举报ID={}", reportId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除举报视频失败");
        }
    }

    @Override
    public JsonResult updateReportVideo(ReportVideos reportVideo) {
        log.debug("更新举报视频信息: {}", reportVideo);

        // 参数校验
        if (reportVideo == null || reportVideo.getReportVideoId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "举报视频ID不能为空");
        }

        if (reportVideo.getVideoUrl() != null && reportVideo.getVideoUrl().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "视频URL不能为空");
        }

        try {
            // 检查举报视频是否存在
            ReportVideos existingReportVideo = reportVideoMapper.selectById(reportVideo.getReportVideoId());
            if (existingReportVideo == null) {
                log.warn("更新举报视频失败，举报视频不存在: ID={}", reportVideo.getReportVideoId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "举报视频不存在");
            }

            int result = reportVideoMapper.updateById(reportVideo);
            if (result > 0) {
                log.info("更新举报视频成功: ID={}", reportVideo.getReportVideoId());
                return JsonResult.ok();
            } else {
                log.warn("更新举报视频失败: ID={}", reportVideo.getReportVideoId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "更新举报视频失败");
            }
        } catch (Exception e) {
            log.error("更新举报视频失败: ID={}", reportVideo.getReportVideoId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "更新举报视频失败");
        }
    }
}
